package com.xiaobaibai.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaobaibai.bo.OrderRecent;
import com.xiaobaibai.common.OrderCommon;
import com.xiaobaibai.common.UserCommon;
import com.xiaobaibai.entity.Announcement;
import com.xiaobaibai.entity.IndexDetail;
import com.xiaobaibai.entity.IndexProduct;
import com.xiaobaibai.entity.Product;
import com.xiaobaibai.response.ResponseFormat;
import com.xiaobaibai.service.IAnnouncementService;
import com.xiaobaibai.service.IIndexDetailService;
import com.xiaobaibai.service.IIndexProductService;
import com.xiaobaibai.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@Api(tags = "首页数据接口")
@RequestMapping("/index")
@CrossOrigin
public class IndexController {

    @Autowired
    private IIndexDetailService indexDetailService;

    @Autowired
    private IIndexProductService indexProductService;

    @Autowired
    private IAnnouncementService announcementService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IProductService productService;

    /**
     * 1.轮播图数据
     * 2.十宫格 大图
     * 3.精品热销
     * 4.新品上市
     * 5.公告
     * 6.近期订单
     */
    @GetMapping("/base-index")
    @ApiOperation("首页基本数据(轮播图+分类宫格+大图+新品+精品+公告+近期订单)")
    public ResponseFormat indexBase(){

        /**
         * 轮播图+九宫格+大图(砍价)
         */
        Map<String, List<IndexDetail>> indexDetailMap = indexDetailService.getIndexDetailMap();

        /**
         * 新品+精品
         */
        Map<String, List<IndexProduct>> indexProductMap = indexProductService.getIndexProductMap();

        /**
         * 公告标题
         */
        List<Announcement> announcementList = announcementService.getAnnouncementTitleList();

        /**
         * 近期订单,存入redis-list然后每天凌晨清理一次
         */
        List<OrderRecent> recentOrderList = redisTemplate.opsForList().
                range(OrderCommon.recentOrder, 0, -1);

        //可砍价商品,是单独一个页面介绍并且介绍商品

        Map<String,Object> map=new HashMap<>();
        map.put("indexDetailMap",indexDetailMap);
        map.put("indexProductMap",indexProductMap);
        map.put("announcementList",announcementList);
        map.put("recentOrderList",recentOrderList);

        return ResponseFormat.ok("首页数据获取成功",map);
    }

    @GetMapping("/guessYouLove")
    @ApiOperation("猜你喜欢")
    public ResponseFormat guessYouLove(@RequestParam(value = "pageNum",required = false,defaultValue = "1")
                                           Integer pageNum){

        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();

        Integer userId= (Integer) authentication.getPrincipal();

        if(pageNum==1){
            //近期足迹
            List<Product> favorites = getFavorites(userId);
            if(favorites!=null&&favorites.size()>=10)
                return ResponseFormat.ok("获取商品数据成功",favorites);
            List<Product> productList = productService.getProductPage(1);
            return ResponseFormat.ok("获取商品数据成功",productList);
        }

        //从商品id1获取数据
        List<Product> productList = productService.getProductPage(pageNum);
        return ResponseFormat.ok("获取商品数据成功",productList);
    }

    private List<Product> getFavorites(Integer userId){
        Set<String> set = stringRedisTemplate.opsForSet().members(UserCommon.favorites + userId);
        if(set==null||set.size()==0)
            return null;

        return productService.listByIds(set);
    }



}
